<?php
	
function list_jadwal_pelatihan(){
	my_set_code_js('
		function confirmDelete(i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?com=jadwal_pelatihan&task=delete&id=\'+i;
		}
		return;	}
	');
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Mulai'=>array('style'=>'border-bottom:2px solid;width:10%'),    
		'Sampai'=>array('style'=>'border-bottom:2px solid;width:10%'),    
		'Nama pelatihan'=>array('style'=>'border-bottom:2px solid;width:50%'),    
		'Kategori'=>array('style'=>'border-bottom:2px solid;width:20%'),    
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
	);

	$query = "SELECT * FROM jadwal_pelatihan a
		INNER JOIN kategori_pelatihan b	ON b.kategori_pelatihan_id = a.kategori_pelatihan_id 
		ORDER BY a.tanggal_mulai
	 ";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		 
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=jadwal_pelatihan&task=edit&id='.$ey['jadwal_pelatihan_id'],
			 
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['jadwal_pelatihan_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'l'=>  date("d-m-Y" , strtotime($ey['tanggal_mulai']))  ,    
			'm'=>  date("d-m-Y" , strtotime($ey['tanggal_selesai']))   ,    
			'ms'=>  $ey['nama_pelatihan']  ,    
			'k'=>  $ey['kategori_pelatihan_label']  ,    
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);		 
	}	
	$datas = table_rows($row);  
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,scrollbars,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel\';'),
		'Tambah data'=>array('onclick'=>'javascript:location.href=\'index.php?com=jadwal_pelatihan&task=edit&id=0\''),
	);	

	$printmenu = additional_menu_on_list( $menuontop ,'');
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu  .table_builder($header , $datas ,  5 , false ,$paging );
}

function form_jadwal_pelatihan($id = 0){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	$view = form_header( "jadwal_pelatihan" , "jadwal_pelatihan"  );
 	$fields = my_get_data_by_id('jadwal_pelatihan','jadwal_pelatihan_id',$id);
	
	$temp = array(
		'name'=>'tanggal_mulai',
		'value'=>( isset($_POST['tanggal_mulai']) ? $_POST['tanggal_mulai'] : ( $fields ? date('d-m-Y',strtotime($fields['tanggal_mulai'])) : date('d-m-Y')  )),
		'id'=>'tanggal_mulai',
		'type'=>'text',
		'size'=>'45'
	);
	$form_temp = form_calendar($temp);
	$view .= form_field_display( $form_temp , "Tanggal Mulai"   );	

	$temp = array(
		'name'=>'tanggal_selesai',
		'value'=>( isset($_POST['tanggal_selesai']) ? $_POST['tanggal_selesai'] : ( $fields ? date('d-m-Y',strtotime($fields['tanggal_selesai'])) : date('d-m-Y')  )),
		'id'=>'tanggal_selesai',
		'type'=>'text',
		'size'=>'45'
	);
	$form_temp = form_calendar($temp);
	$view .= form_field_display( $form_temp , "Tanggal Selesai"   );		

	$temp = array(
		'name'=>'jumlah_jam',
		'id'=>'jumlah_jam',
		'value'=>isset($_POST['jumlah_jam']) ? $_POST['jumlah_jam'] : $fields['jumlah_jam'],
		'size'=>'3',
	);
	$form_temp = form_dynamic($temp);
	$view .= form_field_display( $form_temp.' jam' , "Jumlah Jam" );
	
	$temp = array(
		'name'=>'nama_pelatihan',
		'value'=>( isset($_POST['nama_pelatihan']) ? $_POST['nama_pelatihan'] : $fields['nama_pelatihan'] ),
		'rows'=>'3',
		'cols'=>'45'
	);
	$form_temp = form_textarea($temp);
	$view .= form_field_display( $form_temp , "Nama Pelatihan"   );

	$atemps = array();
	$query = "SELECT * FROM materi_pelatihan ";
	$result = my_query($query);
	while($row = my_fetch_array($result) ){
		$atemps[$row['materi_pelatihan_id']] = $row['materi_pelatihan_label'];
	}
	$temp = array(
		'name'=>'materi_pelatihan_id',
		'value'=>( isset($_POST['materi_pelatihan_id']) ? $_POST['materi_pelatihan_id'] : $fields['materi_pelatihan_id'] ),
		'id'=>'materi_pelatihan_id' 
	);
	$form_materi = form_dropdown($temp , $atemps);
	
	$atemps = array();
	$query = "SELECT * FROM `kategori_pelatihan` ";
	$result = my_query($query);
	while($row = my_fetch_array($result) ){
		$atemps[$row['kategori_pelatihan_id']] = $row['kategori_pelatihan_label'];
	}
	$temp = array(
		'name'=>'kategori_pelatihan_id',
		'value'=>( isset($_POST['kategori_pelatihan_id']) ? $_POST['kategori_pelatihan_id'] : $fields['kategori_pelatihan_id'] ),
		'id'=>'kategori_pelatihan_id' 
	);
	$form_temp = form_dropdown($temp , $atemps);
	$view .= form_field_display( $form_temp .' Materi Pelatihan '.$form_materi , "Kategori Pelatihan"  );

	$atemps = array();
	$query = "SELECT * FROM penyelenggara_pelatihan";
	$result = my_query($query);
	while($row =my_fetch_array($result)){
		$atemps[$row['penyelenggara_id']] = $row['nama'];
	}	
	$temp = array(
		'name'=>'penyelenggara_id',
		'value'=>( isset($_POST['penyelenggara_id']) ? $_POST['penyelenggara_id'] : $fields['penyelenggara_id'] ),
		'id'=>'penyelenggara_id' 
	);
	$form_temp = form_dropdown($temp , $atemps);
	$view .= form_field_display( $form_temp.' &nbsp; <font size="1"><a href="index.php?com='.$_GET['com'].'&task=create_penyelenggara" rel="facebox">[Tambah baru]</a></font>', "Penyelenggara"  ); 
	 
	$atemps = array(
		'In'=> 'Inhouse Training',
		'Ex'=>'Ekstern Training'
	);
	$temp = array(
		'name'=>'tipe_pelatihan',
		'value'=>( isset($_POST['tipe_pelatihan']) ? $_POST['tipe_pelatihan'] : $fields['tipe_pelatihan'] ),
		'id'=>'tipe_pelatihan' 
	);
	$form_temp = form_radiobutton($temp ,$atemps);
	$view .= form_field_display( $form_temp   , "Tipe Pelatihan"   );

	$temp = array(
		'name'=>'tempat_pelatihan',
		'value'=>( isset($_POST['tempat_pelatihan']) ? $_POST['tempat_pelatihan'] : $fields['tempat_pelatihan'] ),
		'id'=>'tempat_pelatihan',
		'rows'=>'4',
		'cols'=>'45'
	);
	$form_temp = form_textarea($temp);	
	$view .= form_field_display( $form_temp  , "Tempat pelatihan"  );   
	
	$temp = array(
		'name'=>'biaya',
		'value'=>( isset($_POST['biaya']) ? $_POST['biaya'] : rp_format($fields['biaya'])),
		'id'=>'biaya',
		'type'=>'text',
		'size'=>'15'
	);
	$form_biaya = form_money($temp);
	$atemps = array('orang'=>'Orang','pelatihan'=>'Pelatihan');
	$temp = array(
		'name'=>'per',
		'value'=>( isset($_POST['per']) ? $_POST['per'] : $fields['per'] ),
		'id'=>'per' 
	);
	$form_per = form_dropdown($temp , $atemps);
	$atemps = array('ptkm'=>'P T K M','sponsor'=>'Sponsor');
	$temp = array(
		'name'=>'penanggung',
		'value'=>( isset($_POST['penanggung']) ? $_POST['penanggung'] : $fields['penanggung'] ),
		'id'=>'penanggung' 
	);

	$form_temp = form_dropdown($temp , $atemps);
	$view .= form_field_display( 'Rp '.$form_biaya .' per '.$form_per.' Ditanggung oleh '.$form_temp , "Biaya"  );

	$temp = array(
		'name'=>'keterangan',
		'value'=>( isset($_POST['keterangan']) ? $_POST['keterangan'] : $fields['keterangan'] ),
		'id'=>'keterangan',
		'rows'=>'4',
		'cols'=>'45'
	);
	$form_temp = form_textarea($temp);	
	$view .= form_field_display( $form_temp  , "Keterangan"  );   

	if($fields['terlampir']=='1'){ 
		$temp = array(
			'name'=>'terlampir',
			'id'=>'terlampir',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$temp = array(
			'name'=>'terlampir',
			'id'=>'terlampir',
			'type'=>'checkbox',
		); 
	}
	$form_temp = form_dynamic($temp).'Brosur Terlampir';
	$view .= form_field_display( $form_temp  , " "  ); 

	$temp = array(
		'value' => ( $id ==0 ? ' Tambah ' :'  Simpan  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($temp); 
	$temp = array(
		'value' => ( 'Kembali'),
		'name' => 'simpan', 
		'type'=>'button',
		'onclick'=>'javascript:location.href=(\'index.php?com='.$_GET['com'].'\')',
		'class'=>'main_button'
	);
	$form_back= form_dynamic($temp); 	
	$view .= form_field_display( $form_submit .' '.$form_back , "&nbsp;" ,  "" );	
	$view .= form_footer( );
	return $view;
} 

function submit_jadwal_pelatihan($id=0){

	$datas = array( 
		'tanggal_mulai'=>my_type_data_str( form_date_converter($_POST['tanggal_mulai']) ),
		'tanggal_selesai'=>my_type_data_str( form_date_converter($_POST['tanggal_selesai']) ),
		'jumlah_jam'=>my_type_data_int($_POST['jumlah_jam']),
		'nama_pelatihan'=>my_type_data_str($_POST['nama_pelatihan']),
		'kategori_pelatihan_id'=>my_type_data_int($_POST['kategori_pelatihan_id']),
		'materi_pelatihan_id'=>my_type_data_int($_POST['materi_pelatihan_id']),
		'penyelenggara_id'=>my_type_data_int($_POST['penyelenggara_id']),
		'tempat_pelatihan'=>my_type_data_str($_POST['tempat_pelatihan']),
		'biaya'=>my_type_data_str( set_text_as_rupiah($_POST['biaya']) ),
		'per'=>my_type_data_str($_POST['per']),
		'penanggung'=>my_type_data_str($_POST['penanggung']),
		'terlampir'=>my_type_data_str( isset($_POST['terlampir']) ? '1' : '0' ),
		'tipe_pelatihan'=>my_type_data_str($_POST['tipe_pelatihan']),
		'keterangan'=>my_type_data_str($_POST['keterangan']),
		'datetime_added'=>my_type_data_function('NOW()')     
	); 
	if($id > 0){
		return my_update_record('jadwal_pelatihan' ,'jadwal_pelatihan_id' , $id , $datas);
	}
	
	return my_insert_record('jadwal_pelatihan' , $datas);
}

function delete_jadwal($id){
	my_query( "DELETE FROM karyawan_pelatihan WHERE jadwal_pelatihan_id = {$id}") ;
	my_query( "DELETE FROM pelatihan_evaluasi_entry WHERE jadwal_pelatihan_id = {$id}") ;
	$query= "DELETE FROM jadwal_pelatihan WHERE jadwal_pelatihan_id = {$id}";
	return my_query($query);
} 


function excel_report(){
	my_component_load('xl_builder' , false);
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Mulai'=>array('style'=>'border-bottom:2px solid;width:10%'),    
		'Sampai'=>array('style'=>'border-bottom:2px solid;width:10%'),    
		'Nama pelatihan'=>array('style'=>'border-bottom:2px solid;width:55%'),    
		'Kategori'=>array('style'=>'border-bottom:2px solid;width:20%'),    
	);

	$query = "SELECT * FROM jadwal_pelatihan a
		INNER JOIN kategori_pelatihan b	ON b.kategori_pelatihan_id = a.kategori_pelatihan_id
		ORDER BY a.tanggal_mulai
	";

	$result = my_query($query ); 
	$row = array();  
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'l'=>  date("d-m-Y" , strtotime($ey['tanggal_mulai']))  ,    
			'm'=>  date("d-m-Y" , strtotime($ey['tanggal_selesai']))   ,    
			'ms'=>  $ey['nama_pelatihan']  ,    
			'k'=>  $ey['kategori_pelatihan_label']  ,    
		);		
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 15 ,false ); 
}

function print_plain(){
	
	$query = "SELECT * FROM jadwal_pelatihan a
		INNER JOIN kategori_pelatihan b	ON b.kategori_pelatihan_id = a.kategori_pelatihan_id
		ORDER BY a.tanggal_mulai
	";
	$result = my_query($query ); 

	$datas[] = array(
		 'k1'=>array("No." ,5 ,STR_PAD_BOTH),
		 'k2'=>array("Mulai" ,10,STR_PAD_RIGHT),
		 'k3'=>array("Sampai" ,10,STR_PAD_RIGHT),
		 'k4'=>array("Nama Pelatihan" ,55,STR_PAD_RIGHT),  
		 'k5'=>array("Kategori" ,20,STR_PAD_RIGHT),  
	); 
	$datas[] = array(	//FOR HEADER SPACE (NECK)
		 'k1'=>array(" " ,100,STR_PAD_BOTH),
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;

		$datas[] = array(		 
			'k1'=>array( $i ,5 ,STR_PAD_BOTH),
			'k2'=>array( date("d-m-Y" , strtotime($ey['tanggal_mulai'])),10,STR_PAD_RIGHT),
			'k3'=>array( date("d-m-Y" , strtotime($ey['tanggal_selesai'])) ,10,STR_PAD_RIGHT),
			'k4'=>array( $ey['nama_pelatihan'],55,STR_PAD_RIGHT),  
			'k5'=>array($ey['kategori_pelatihan_label'] ,20,STR_PAD_RIGHT),  
		);
	}
	$view = 	str_pad("DEVELOPMENT SELECTION",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}

 